package bTree.LeetCode;

import java.util.Stack;

public class 逆波兰表达式求值 {
    public int evalRPN(String[] tokens) {
        //定义一个栈
        Stack<String>stack=new Stack<>();
        for (String token : tokens) {
            if(token.equals("+")){
                String num1=stack.pop();
                String num2=stack.pop();
                stack.push(Integer.toString(Integer.parseInt(num1)+Integer.parseInt(num2)));
                continue;
            }if(token.equals("-")){
                String num1=stack.pop();
                String num2=stack.pop();
                stack.push(Integer.toString(Integer.parseInt(num1)-Integer.parseInt(num2)));
                continue;
            }if(token.equals("*")){
                String num1=stack.pop();
                String num2=stack.pop();
                stack.push(Integer.toString(Integer.parseInt(num1)*Integer.parseInt(num2)));
                continue;
            }if(token.equals("/")){
                String num1=stack.pop();
                String num2=stack.pop();
                stack.push(Integer.toString(Integer.parseInt(num1)/Integer.parseInt(num2)));
                continue;
            }
            stack.push(token);
        }
        return Integer.parseInt(stack.pop());
    }
}